-
-
Notifications
You must be signed in to change notification settings - Fork 6.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
requires to access .default
after build while it's not required in dev (CJS dependency)
#2139
Comments
Not encountered this error. Can you have a try after upgrade vite? |
This comment was marked as outdated.
This comment was marked as outdated.
I ran into the same issue using latest Vite version. NoteThe title of the issue is very vague as this error is displayed by minified React instead of ANY React error. So it can be anything really. The first thing to do in this case is to debug the app in order to have more details: Use minify and dev mode in your
Hacky solution in my caseIn my case it was coming from using
Basically this is the content of the import The react-switch library is built as In the reproduction repoIf you follow the debugging steps, you will discover that the culprit in your case is react-highlight Notes for reproductionIn order to reproduce the issue, you need to:
|
It's because esbuild is checking for __esModule at runtime and rollup is not. So it will happen to any library that has an entry point with
Since this file does not include the 'magic string'
So need to raise a bug in rollup for this. |
Unsurprisingly, it's a known issue mentioned in rollup/plugins#481 but it doesn't seem like there will be a solution very soon. One thing to note is that the node behaviour is mutually exclusive with supporting __esModule. When running natively from Node, |
Then we should issue a big warning at dev time about this behaviour. It's a critical bug (in the libraries) that affects the Vite experience drastically. |
Yup, same problem here: #2692. |
After looking into this some more, I think vite should switch to behave the same as rollup. That way it would be consistent with how node works (and snowpack as well). For now, importing a commonjs module should probably show a warning somewhere. |
@ntkoopman would you expand a bit in what needs to be changed in Vite? A PR would be great so we can discuss what the implications are there (from anyone that is following this issue) Agree about the warning in the meantime in case that change is not easy to land, PR welcome for that patch too |
So I already changed my mind again, since changing the dev behaviour will still break dependencies. So, the best case would still be that it gets fixed in rollup. I tested with a commonjs plugin I wrote and it seems to work for us for now: import cjs from 'rollup-plugin-cjs';
export default defineConfig({
build: {
rollupOptions: {
input: {
index: resolve(__dirname, 'src/index.html'),
},
plugins: [cjs()],
},
commonjsOptions: {
exclude: [/./],
},
},
}); I looked into returning a warning, but decided not to, because if it's possible to detect the issue it should be possible to fix it.. I'll continue on the commonjs plugin and see if I can fix it there (or maybe it's just a configuration option that's missing?) now that I have some unit tests. |
I used this to fix this issue cropping up with react-no-ssr. |
This issue is complete blocker for me from using Vite in my React production apps. |
Is there an ETA on at least catching this some way in development? |
I'm blocked by the same problem. How are people deploying their apps into production? Is there a workaround in the meantime? |
To track down which package was causing the problem, I had to do some grindy work of setting breakpoints in Chrome dev tools (on the error-throwing lines, and in some cases going up the call stack) and then iterating through until I found the culprit. Not an easily reproducible solution, especially since in most cases you're dealing with minified code. |
This comment was marked as off-topic.
This comment was marked as off-topic.
This comment was marked as off-topic.
This comment was marked as off-topic.
I have some problem, but I use nextjs with yarn, im not using Vite any solutions? |
Nextjs has no connection to vite, so it's a separate issue. I recommend you look for it on nextjs's github or ask for help on some discord server in the future. This issue has a lot of subscribers so it's best not to notify all of them needlessly One generic workaround is to use
If you have further questions feel free to hit me up on Discord filipw01#2360 |
(to be clear this isn't a faulty package, it's how most things on npm work - it's just CJS) |
with @mdi/react |
You shouldn't need this if you're using the vite types: https://vitejs.dev/guide/features.html#client-types
You shouldn't need to cast to |
Hello. Also encountered error 130 when building the project on Vite. Found such a working solution in the documentation to add the following settings to viteconfig: optimizeDeps: { |
This worked. Thank you |
#8743 was merged June 24th. I guess this is fixed now, right? |
Any update on this ? I am facing similar issue when using a package with default export https://www.npmjs.com/package/markdown-to-text and changing the export to named export solves the issue. |
Are you hitting this issue for browser or SSR? I know there's still the inconsistency in SSR (which I want to look into for Vite 5), but I'm not sure about browsers if the comment above mentioned #8743 fixes it? |
@ljharb I am facing the issue in browser. Changing the above mentioned library to named export fixes the issue. I am using vite version |
Updating vite to 4.4.9 has fixed this for me |
I can actually confirm that See also |
I've tried all the repros and packages mentioned here and I cannot reproduce the issue today (Vite 4.5.0). I'll close this as fixed. If you still see the issue, please leave a comment with a repro, or open a new issue. Thanks! |
亲,你的信我已收到,真希望尽快看到它!
|
Describe the bug
Encountered “Minified React error” in the production environment, the dev environment is fine.
Reproduction
Repo: https://github.com/songquanpeng/snippet-manager/tree/aa02a582c676bd3dcd5254f18d7087bb8a1a7220
My React application is on the
web
folder.System Info
vite
version: 2.0.0Summarization of the root problem
#2139 (comment)
The text was updated successfully, but these errors were encountered: